package fr.inserm.u915.tabixserver;

import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;

import fr.inserm.u915.tabixserver.expr.ExprNode;

public class Column extends Named
	{
	private int column_index;
	private boolean ignore=false;
	
	private AtomicType dataType=AtomicType.STRING;
	private ExprNode urlExpr;
	
	public Column()
		{
		
		}
	
	void setDataType(AtomicType dataType)
		{
		this.dataType = dataType;
		}
	
	public AtomicType getDataType()
		{
		return dataType==null?AtomicType.STRING:dataType;
		}
	
	public int getColumnIndex()
		{
		return column_index;
		}


	void setColumnIndex(int column_index)
		{
		this.column_index = column_index;
		}

	void setUrlExpr(ExprNode urlExpr) {
		this.urlExpr = urlExpr;
		}
	
	public ExprNode getUrlExpr() {
		return urlExpr;
		}
	
	public boolean isIgnore()
		{
		return ignore;
		}

	void setIgnore(boolean ignore)
		{
		this.ignore = ignore;
		}
	boolean looksLikeChrom()
		{
		if(getDataType()!=AtomicType.INT && getDataType()!=AtomicType.STRING) return false;
		String s=getId().toLowerCase();
		return s.equals("chr") || s.equals("chrom") || s.equals("chromosome") || s.equals("#chrom");
		}
	boolean looksLikeStart()
		{
		if(getDataType()!=AtomicType.INT ) return false;
		String s=getId().toLowerCase();
		return 	s.equals("start") ||
				s.equals("chromstart") ||
				s.equals("txstart") ||
				s.equals("pos")
				;
		}
	boolean looksLikeEnd()
		{
		if(getDataType()!=AtomicType.INT ) return false;
		String s=getId().toLowerCase();
		return s.equals("end") || s.equals("chromend") || s.equals("txend") || s.equals("pos");
		}
	boolean looksLikeName()
		{
		String s=getId().toLowerCase();
		return s.equals("name");
		}
	boolean looksLikeStrand()
		{
		String s=getId().toLowerCase();
		return s.equals("strand");
		}
	
	void writeXsd(XMLStreamWriter w) throws XMLStreamException
	    {
		w.writeStartElement(XSD.prefix,"element",XSD.NS);
		
		w.writeAttribute("name",getId());
		w.writeAttribute("type",getDataType().getXsdType());
		XSD.writeAnnotation(w, getDescription());
		w.writeEndElement();
	    }

	void writeSoap(XMLStreamWriter w)throws XMLStreamException
		{
		w.writeStartElement("columnDef");
		
		w.writeStartElement("id");
		w.writeCharacters(this.getId());
		w.writeEndElement();

		w.writeStartElement("label");
		w.writeCharacters(this.getLabel());
		w.writeEndElement();
		
		w.writeStartElement("description");
		w.writeCharacters(this.getDescription());
		w.writeEndElement();

		w.writeEndElement();
		}

	
	}
